package com.rmdc.simop.commons.controller.crud;

import java.io.Serializable;

import javax.faces.component.html.HtmlCommandButton;
import javax.faces.event.ActionEvent;

import org.ajax4jsf.component.html.HtmlAjaxCommandButton;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.rmdc.simop.commons.controller.BaseBean;
import com.rmdc.simop.commons.model.service.BaseService;

public abstract class BaseCrudJSFBean<Entity extends Serializable> extends
		BaseBean {

	protected java.util.List<String> criteriosFiltro;

	private static final Log log = LogFactory.getLog(BaseCrudJSFBean.class);

	protected HtmlAjaxCommandButton botonNuevo;
	protected HtmlAjaxCommandButton botonBuscar;
	protected HtmlAjaxCommandButton botonModificar;
	protected HtmlAjaxCommandButton botonEliminar;
	protected HtmlCommandButton botonAplicar;
	protected HtmlCommandButton botonCancelar;
	protected HtmlCommandButton botonLimpiar;
	protected String accionSeleccionada;
	



	
	
	public static final String ACCION_NUEVO = "Ingresar nuevo elemento";
	public static final String ACCION_ELIMINAR = "Eliminar elemento seleccionado";
	public static final String ACCION_BUSCAR = "Buscar un elemento";
	public static final String ACCION_MODIFICAR = "Modificar el elemento actual";
	public static final String ACCION_NULA = "Ninguna";

	
	
	
	public Entity entity = getNewEntity();

	protected abstract Entity getNewEntity();

	protected abstract String getMessagesSuccess();

	public BaseCrudJSFBean() {
		botonNuevo = new HtmlAjaxCommandButton();
		botonNuevo.setRendered(true);

		botonBuscar = new HtmlAjaxCommandButton();
		botonBuscar.setRendered(true);

		botonModificar = new HtmlAjaxCommandButton();
		botonModificar.setRendered(false);

		botonEliminar = new HtmlAjaxCommandButton();
		botonEliminar.setRendered(false);

		botonAplicar = new HtmlCommandButton();
		botonCancelar = new HtmlCommandButton();
		botonLimpiar = new HtmlCommandButton();

	

		accionSeleccionada = ACCION_NULA;
	}

	public void nuevoEvent(ActionEvent event) {
		log.info("BOTONERA: Entrando a AccionNuevo");
		botonBuscar.setRendered(false);
		botonEliminar.setRendered(false);
		botonModificar.setRendered(false);
		accionSeleccionada = ACCION_NUEVO;
	}



	public void modificarEvent(ActionEvent event) {

	}

	public void eliminarEvent(ActionEvent event) {

	}

	public void AplicarFiltroEvent(ActionEvent event) {
		// TODO- Hacer
	}

	

	public String aplicarAction() {
		log.info("BOTONERA: Entrando a AccionAplicar");

		if (accionSeleccionada.equals(ACCION_NUEVO)) {
			log.info("BOTONERA: ejecutando ACCION_NUEVO");
			try {
				baseService.create(entity);
			} catch (Exception e) {
				agregarMensajeError("ERROR CREANDO_ LLENAR EL PROPERTIES");
				e.printStackTrace();
				return null;
			}
		} else if (accionSeleccionada.equals(ACCION_BUSCAR)) {
			log.info("BOTONERA: ejecutando ACCION_BUSCAR");
		} else if (accionSeleccionada.equals(ACCION_ELIMINAR)) {
			log.info("BOTONERA: ejecutando ACCION_ELIMINAR");
		} else if (accionSeleccionada.equals(ACCION_MODIFICAR)) {
			log.info("BOTONERA: ejecutando ACCION_MODIFICAR");
		} else {
			log.info("BOTONERA: ejecutando ACCION_DESCONOCIDA");
			agregarMensajeInformacion("ACCION_DESCONOCIDA");
		}

		agregarMensajeInformacion("TODO_FINO, AGREGAR AL PROPERTIES");
		entity = getNewEntity();
		return null;
	}

	public String cancelarAction() {
		log.info("BOTONERA: Entrando a AccionCancelar");
		botonBuscar.setRendered(true);
		botonBuscar.setRendered(true);
		botonEliminar.setRendered(false);
		botonModificar.setRendered(false);
		accionSeleccionada = ACCION_NULA;
		return null;
	}

	public BaseService<Entity> baseService;

	@SuppressWarnings("unchecked")
	public Entity getEntity() {
		return entity;
	}

	public void setEntity(Entity entity) {
		this.entity = entity;
	}

	public BaseService<Entity> getBaseService() {
		return baseService;
	}

	public void setBaseService(BaseService<Entity> baseService) {
		this.baseService = baseService;
	}

	public HtmlCommandButton getBotonAplicar() {
		return botonAplicar;
	}

	public void setBotonAplicar(HtmlCommandButton botonAplicar) {
		this.botonAplicar = botonAplicar;
	}

	public HtmlAjaxCommandButton getBotonBuscar() {
		return botonBuscar;
	}

	public void setBotonBuscar(HtmlAjaxCommandButton botonBuscar) {
		this.botonBuscar = botonBuscar;
	}

	public HtmlCommandButton getBotonCancelar() {
		return botonCancelar;
	}

	public void setBotonCancelar(HtmlCommandButton botonCancelar) {
		this.botonCancelar = botonCancelar;
	}

	public HtmlAjaxCommandButton getBotonEliminar() {
		return botonEliminar;
	}

	public void setBotonEliminar(HtmlAjaxCommandButton botonEliminar) {
		this.botonEliminar = botonEliminar;
	}

	public HtmlCommandButton getBotonLimpiar() {
		return botonLimpiar;
	}

	public void setBotonLimpiar(HtmlCommandButton botonLimpiar) {
		this.botonLimpiar = botonLimpiar;
	}

	public HtmlAjaxCommandButton getBotonModificar() {
		return botonModificar;
	}

	public void setBotonModificar(HtmlAjaxCommandButton botonModificar) {
		this.botonModificar = botonModificar;
	}

	public HtmlAjaxCommandButton getBotonNuevo() {
		return botonNuevo;
	}

	public void setBotonNuevo(HtmlAjaxCommandButton botonNuevo) {
		this.botonNuevo = botonNuevo;
	}

	public java.util.List<String> getCriteriosFiltro() {
		return criteriosFiltro;
	}

	public void setCriteriosFiltro(java.util.List<String> criteriosFiltro) {
		this.criteriosFiltro = criteriosFiltro;
	}

	public String getAccionSeleccionada() {
		return accionSeleccionada;
	}

	public void setAccionSeleccionada(String accionSeleccionada) {
		this.accionSeleccionada = accionSeleccionada;
	}

}